package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.PkgcartingForm;
import com.vcaresolution.logistic.to.PkgcartingTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>Pkgcarting Controller</code> is used to track all pkgcarting related actions
 *
 * @author Dharam C. Gusai
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class PkgcartingController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.PKGCARTINGFORM_MODELATTRIBUTE)
	public PkgcartingForm createForm(){
		return new PkgcartingForm();
	}
		
	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){
		modelAndView.addObject(ApplicationConstants.JOBREGISTERLIST_MODELATTRIBUTE, getServiceRegistry().getJobRegisterService().findByAll());
	}
	
	/**
	 * This method is called when the GET request has been made to the ADD Pkgcarting Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_PKGCARTING_URL,method = RequestMethod.GET)
	public ModelAndView loadAddPkgcarting() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_PKGCARTING_VIEW);
		modelAndView.addObject(ApplicationConstants.PKGCARTINGFORM_MODELATTRIBUTE, new PkgcartingForm());
			
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD Pkgcarting Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_PKGCARTING_URL,method = RequestMethod.POST)
	public ModelAndView processAddPkgcarting(@Valid PkgcartingForm pkgCartingForm, 
			 final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			
			pkgCartingForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getPkgcartingService().add(pkgCartingForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_PKGCARTING_URL, true, true, false));
		} else {
			
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the List Pkgcarting Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_PKGCARTING_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<PkgcartingTO> pkgcartingList = getServiceRegistry().getPkgcartingService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.PKGCARTINGLIST_MODELATTRIBUTE, pkgcartingList);
		modelAndView.setViewName(ApplicationConstants.LIST_PKGCARTING_VIEW);
	
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADD Pkgcarting Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_PKGCARTING_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditPkgcarting(@RequestParam(value="pkgcartingId") String pkgcartingId) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object pkgcartingObject = getServiceRegistry().getPkgcartingService().findById(Integer.parseInt(pkgcartingId));
		loadRequiredDetailInModelObject(modelAndView);
		
		modelAndView.addObject(ApplicationConstants.PKGCARTINGFORM_MODELATTRIBUTE, pkgcartingObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_PKGCARTING_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD Pkgcarting Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_PKGCARTING_URL,method = RequestMethod.POST)
	public ModelAndView processEditpkgcarting(@Valid PkgcartingForm pkgCartingForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			
			pkgCartingForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getPkgcartingService().update(pkgCartingForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_PKGCARTING_URL, true, true, false));
		} 
		else {
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
}